<template>
	<view class="broadcast_mid">
		<view v-if="giftShow >0">
			<view class="gift">
				<image class="gift_left" @tap="() => { this.onManage = true }" :src="gift.avatarUrl" />
				<view class="gift_con">
					<text class="text-white fs-30 ">
						{{gift.nickname}}
					</text>
					<text class="text-white fs-30 ">
						送{{gift.name}}
					</text>
				</view>
			</view>
			<view class="gift_item text-white">
				<image class="back" :src="gift.image" />
				<text class="gift_num">X {{gift.num}}</text>
			</view>
		</view>
		<view class="box">
			<scroll-view :scroll-top="scrollTop" scroll-y="true" class="scroll-Y scrolling"
				scroll-with-animation="true">
				<view id="scroll-view-content">
					<view class="scrolling_item" v-for="(item, index) in list" :key="index">
						<view class="title1">
							<view>
								<text class="pink fs-26">{{ item.user }}：</text>
							</view>
							<text class="text-white title2 fs-26">{{ item.content }}</text>
						</view>
					</view>
				</view>
			</scroll-view>
			<view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				gift:{
					nickname:'',
					avatarUrl:"",
					name:"",
					image:"",
					num:"0"
				},
				giftList:[],
				giftShow:0,
				list: [],
				scrollTop: 0,
				scrollViewHeight: 0,
				xxx: false
			}
		},
		mounted() {
			var that = this
			uni.$on('WsMessage', function(res) {
				that.WsMessage(res)
			})
			setInterval(function(){
				if(that.giftShow > 0)
				{
				
					that.giftShow = that.giftShow - 1	
				}
			},1000)
		},
		methods: {
			WsMessage(data) {
				
				switch (data.type) {
				case 'message':
					this.listPush(data);
				break;
				
				case 'gift':
					this.giftlistPush(data);
				break;
				
				}
			},
			
			giftlistPush(data) {
				console.log(7777,data)
				this.gift  = {
					nickname: data.user.nickName,
					avatarUrl: data.user.avatarUrl,
					name:data.gift.title,
					image:data.gift.cover_svg,
					num:data.num,
				}
				this.giftShow = 10
				
			},
			
			listPush(data) {
				this.list.push({
					user: data.user.nickName,
					content: data.content
				})
				this.scrollToBottom()
			},
			scrollToBottom() {
				this.$nextTick(() => {
					uni.createSelectorQuery().in(this).select('#scroll-view-content').boundingClientRect((res) => {
						let top = res.height - this.scrollViewHeight;
						if (top > 0) {
							this.scrollTop = top + 350;
						}
					}).exec()
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.text-white {
		color: #fff;
	}

	.fs-26 {
		font-size: 26rpx;
	}

	.fs-30 {
		font-size: 30rpx;
	}

	//礼物弹幕部分
	.broadcast_mid {
		position: fixed;
		bottom: 150rpx;
		height: 500rpx;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
	}

	.gift {
		width: 300rpx;
		height: 100rpx;
		margin-left: 30rpx;
		border-radius: 50rpx;
		display: flex;
		flex-direction: row;
		align-items: center;
		padding: 5rpx 10rpx;
		background-color: $uni-bg-color-mask;
	}

	.gift_left {
		width: 80rpx;
		height: 80rpx;
		border-radius: 40rpx;
	}

	.gift_con {
		width: 170rpx;
		height: 80rpx;
		margin-left: 8rpx;
		display: flex;
		flex-direction: column;
		justify-content: center;
	}

	.gift_item {
		position: fixed;
		bottom: 580rpx;
		left: 250rpx;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
	}

	.back {
		width: 100rpx;
		height: 100rpx;
	}

	.gift_num {
		color: #fff;
		font-size: 70rpx;
	}

	.box {
		height: 350rpx;
		display: flex;
		flex-direction: column;
	}

	.scroll-view-content {
		height: 350rpx;
	}

	.scrolling {
		height: 350rpx;
		max-width: 600rpx;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		margin-left: 30rpx;
	}

	.pink {
		color: pink;
		min-width: auto;
	}

	.scrolling_item {
		min-height: 45rpx;
		display: flex;
		flex-direction: row;
		align-items: baseline;
		margin: 5rpx 0;
	}

	.title1 {
		max-width: 600rpx;
		padding: 3px 10px;
		border-radius: 35rpx;
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: baseline;
		margin: 5rpx 0;
		background-color: $uni-bg-color-mask;
	}

	.title2 {
		max-width: 500rpx;
		border-radius: 0 45rpx 45rpx 0;
		word-break: break-all;
		white-space: pre-line;
	}
</style>